/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;

import dao.DanhMucDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.DanhMuc;

/**
 *
 * @author OSIRIS
 */
public class DanhMucFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form DanhMucFrame
     */
    public DanhMucFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDanhMuc = new javax.swing.JTable();
        jBtnThem = new javax.swing.JButton();
        jBtnXoa = new javax.swing.JButton();
        jBtnCapNhat = new javax.swing.JButton();

        setClosable(true);
        setTitle("Danh Sach Danh Muc");

        jTableDanhMuc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableDanhMuc.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                jTableDanhMucAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(jTableDanhMuc);

        jBtnThem.setText("Thêm");
        jBtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnThemActionPerformed(evt);
            }
        });

        jBtnXoa.setText("Xóa");
        jBtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnXoaActionPerformed(evt);
            }
        });

        jBtnCapNhat.setText("Cập Nhật");
        jBtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCapNhatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnThem, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBtnXoa, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBtnCapNhat, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnThem)
                        .addGap(29, 29, 29)
                        .addComponent(jBtnXoa)
                        .addGap(32, 32, 32)
                        .addComponent(jBtnCapNhat))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(63, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void load() {
        //List<DanhMuc> ds=DanhMucDAO.getList();
        DanhMucDAO dmDao = new DanhMucDAO();
        List ds = dmDao.findAll();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("ID");
        dtm.addColumn("Ten Danh Muc");
        for (int i = 0; i < ds.size(); i++) {
            DanhMuc dm = (DanhMuc) ds.get(i);
            Vector vt = new Vector();
            vt.add(dm.getId());
            vt.add(dm.getTen());
            dtm.addRow(vt);
        }
        this.jTableDanhMuc.setModel(dtm);
    }
    private void jTableDanhMucAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jTableDanhMucAncestorAdded
        // TODO add your handling code here:

        load();

    }//GEN-LAST:event_jTableDanhMucAncestorAdded

    private void jBtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnThemActionPerformed
        // TODO add your handling code here:
        try {
            DanhMucThemDialog dlg = new DanhMucThemDialog(null, true);
            dlg.setTitle("Thêm");
            dlg.setVisible(true);
            DanhMucDAO dmDAO = new DanhMucDAO();
            DanhMuc dm = new DanhMuc();
            dm.setTen(dlg.getTenDanhMuc());
            dmDAO.create(dm);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }

    }//GEN-LAST:event_jBtnThemActionPerformed
    private int getDmIdToTable() {
        int selectedRowIndex = jTableDanhMuc.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        int selectedObject = (Integer) jTableDanhMuc.getModel().getValueAt(selectedRowIndex, 0);
        return selectedObject;
    }

    private String getDmTenToTable() {
        int selectedRowIndex = jTableDanhMuc.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        String selectedObject = (String) jTableDanhMuc.getModel().getValueAt(selectedRowIndex, 1);
        return selectedObject;
    }
    private void jBtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnXoaActionPerformed
        // TODO add your handling code here:
        try {
            DanhMucDAO dmDAO = new DanhMucDAO();
            DanhMuc dm = new DanhMuc();
            dm.setId(this.getDmIdToTable());
            dmDAO.delete(dm);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }

    }//GEN-LAST:event_jBtnXoaActionPerformed

    private void jBtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCapNhatActionPerformed
        // TODO add your handling code here:
        try {
            DanhMucThemDialog dlg = new DanhMucThemDialog(null, true);
            dlg.setTitle("Cập Nhật");
            dlg.setMaDanhMuc(this.getDmIdToTable());
            dlg.setTenDanhMuc(this.getDmTenToTable());
            dlg.setVisible(true);
            DanhMucDAO dmDAO = new DanhMucDAO();
            DanhMuc dm = new DanhMuc();
            dm.setId(dlg.getMaDanhMuc());
            dm.setTen(dlg.getTenDanhMuc());
            dmDAO.update(dm);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }

    }//GEN-LAST:event_jBtnCapNhatActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCapNhat;
    private javax.swing.JButton jBtnThem;
    private javax.swing.JButton jBtnXoa;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDanhMuc;
    // End of variables declaration//GEN-END:variables
}
